$('.right-titleicon').on("click",function(e){
	projectVM.showMenulist()
})
mui.plusReady(function () {
})
let that;
projectVM = new Vue({
	el: "#content",
	components:{ 
		personselect,
		selectitem
	},
	data: {
		isLumpFirst:false,
		isLumpFirstAdd:true,
		dtPickrer:'',
		showSelect:false,
		isSingle:false,
		menuList:[
			{name:'投入',path:'goIn.html'},{name:'退出',path:'goOut.html'},{name:'列表',path:'index.html'}
		],
		typeList:[{name:'变压器1'},{name:'变压器2'},{name:'变压器3'},{name:'变压器4'},],
		fileList:[],
		showSubmit:false,
		onStatus:'',
		selectText:'变压器1',
		result:'',
		showMenu:false,
		showMorebtn:false,
		advance:'',
		showAdd:false,
		show_popup:false,
		inName:"",//投入执行人
		intutelageStaffName:"",//投入监护人
		onlyselect:false,
		mDisable:false,
		url:"",
		netData:[],
		newData:[],
		fileIds:"",//文件上传 文件id列表
		list:[
			{
				isLumpFirst:false,
				isLumpFirstAdd:true,
				plateCode:'',
				plateName:'',
				deviceName:"",
				outCause:"",
				outTime:"",
			},
		]
	},
	created: function() {
		that = this;
		that.list[0].outTime = that.dateFormate()
		let userdata =  that.getUserInfo();
		that.inName = userdata.userName;
	},
	methods: {
		//选择投入时间
		chooseDate(position){
			debugger
			this.dtPickrer.show(function(e){
				console.log(JSON.stringify(e),'==')
				that.list[position].outTime = e.value;
			},false)
		},
		// 获取时间
		dateFormate () {
			return mui.neiosCommon.transTime(new Date(),"min")
		},
		//获取毫秒
		getMiu(){
			let date = that.dateFormate();
			var arr = date.split(/[- : \/]/);
			var startDate = Date.parse(new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4]));
			return startDate;
		},
		//获取uuid
		getUUid(){
			let uuid = mui.neiosCommon.getUuidv4();
			return uuid;
		},
		//获取用户信息-新建投退记录的情况下
		getUserInfo(){
			let user = JSON.parse(localStorage.getItem("neiosUser"))
			return user;
		},
		//保护压板编号回调
		itemChange(position){
			let GV = 'selectitem'+position
			let chooseData = this.$refs[GV][0].chooseItem;
			that.list[position].deviceName = chooseData.deviceName;
			that.list[position].deviceCode = chooseData.deviceCode;
			console.log(JSON.stringify(chooseData))
		},
		//选择变压器类型
		chooseType(e){
			console.log(e,'===')
		},
		//返回上一页
		goBack(){
			let pageId = plus.webview.getWebviewById("dailyRecord/platenProtect/index.html");
			mui.fire(pageId, 'reloadPage', {result:"result"});
			mui.back()
		},
		//更多按钮
		showMore(){
			this.showMorebtn ? this.showMorebtn = false : this.showMorebtn = true
		},
		//阻止事件冒泡
		clickStop(){

		},
		//选择是否为值班人
		checkOnstatus(){
			console.log(this.onStatus)
			this.onStatus ? this.onStatus = false : this.onStatus =true
			
		},
		//点击菜单
		menuClick(e){
			if(e.name == '我值班'){
				this.onStatus? this.onStatus= false : this.onStatus=true
			}else{
				this.navTo(e.path)
			}
		},
		//左上角菜单显示隐藏
		showMenulist(){
			this.showMenu?this.showMenu=false:this.showMenu=true
		},
		//测试下载
		// base64转blob
		base64ToBlob(base64Data) {
			let arr = base64Data.split(','),
			// fileType = arr[0].match(/:(.*?);/)[1],
			bstr = atob(arr[0]),
			l = bstr.length,
			u8Arr = new Uint8Array(l);
		                
			while (l--) {
				u8Arr[l] = bstr.charCodeAt(l);
			}
			return new Blob([u8Arr], {
				type: ".xls"
		    });
		},
		navTo(e){
			mui.openWindow({
					url: e,
					id: e,
			});
		},
		//添加新的检查单
		addLupm(){
			this.list.forEach( (item,i) =>{
				if(i == 0){
					item.isLumpFirst = true;
					item.isLumpFirstAdd = false;
				}
			})
			let newLumpItem = {
				isLumpFirst:true,
				isLumpFirstAdd:true,
				plateCode:'',
				plateName:'',
				deviceName:"",
				outCause:"",
				outTime:"",
			}
			this.list.push(newLumpItem)
		},
		//移除检查单
		removeLupm(position){
			
			//移除页面传过来的对应的 item 
			// if(that.exitDataJson != ""){
			// 	that.exitDataJson.netDeviceInfo.forEach((item,i)=>{
			// 		if(item.deviceInfoId == this.list[position].deviceInfoId){
			// 			that.exitDataJson.netDeviceInfo.splice(i,1);
			// 		}
			// 	})
			// }
			this.list.splice(position,1)
			if(this.list.length <= 1){
				this.list[0].isLumpFirst = false;
				this.list[0].isLumpFirstAdd = true;
			}else{
				let lumpItemPosition = this.list.length;
				this.list[lumpItemPosition - 1].isLumpFirstAdd = true;
			}
		},
		// 提交
		subData(){
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.submitPlateOperate;
			that.url = url;
			that.createData(32);
		},
		// 保存
		saveData(){
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.saveOrUpdatePlateOperate;
			that.url = url;
			that.createData(21);
		},
		//组装数据
		createData(status){
			let message = status == "21" ? "是否保存" : "是否提交";
			mui.confirm(message,'提示',['取消','确认'],function (e) {
				if(1 == e.index){
					let manTab = paramJson.getManTab(that,status,"out")
					let dataObj = paramJson.getInfoList(that.list,manTab,"out")
					that.netData = dataObj.infoListParam;
					dataObj.workType = "0208";
					console.log("+++++++"+JSON.stringify(dataObj))
					that.devoiceCheck(that.netData[0],function(ret,data){
						console.log("KKKK:"+JSON.stringify(ret))
						console.log("JJJJ:"+JSON.stringify(data))
						dataObj.infoListParam = data;
						if(!ret){
							that.doInteNet(dataObj);
						}
					});
				}
			},'div')
		},
		
		
		//校验 设备编码
		devoiceCheck(data,fun){
			const param = {}
			param.plateCode = data.plateCode;//保护压板编号
			param.operateType = "0";//0, 投入是1退出是0
			// param.outOperateId = data.outOperateId;//退出ID 退出时传
			param.inOperateId = data.inOperateId;//投入ID 投入时传
			param.operateStaffCode = that.getUserInfo().userId;//操作人工号
			param.stationCode = data.newStationCode;//电厂编码
			console.log("LLL:"+JSON.stringify(param))
			
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.checkCodeValidate;
			
			mui.neiosRequst.httpRequst(url,param,function(ret){
				console.log(JSON.stringify(ret))
				if(ret.code == 0){
					if(ret.isRepeat == false){
						data.plateOperateInfoId = ret.plateOperateInfoId == "" ? that.getUUid() : ret.plateOperateInfoId;
						
						that.newData.push(data);
						that.netData.splice(0,1)
						if(that.netData.length > 0){
							that.devideCodeCheck(that.netData[0],fun)
						}else{
							fun(ret.isRepeat,that.newData)
						}
					}else{
						mui.alert("编号"+data.plateCode+"无法进行退出")
					}
				}
				if( ret.code == 1){
					mui.toast(ret.msg)
				}
			},function(err){
				console.log("失败回掉："+JSON.stringify(err))
			})
		},
		
		//网络请求
		doInteNet(params){
			let isNull = mui.neioConfig.checkPlatenList(params.infoListParam,"out")
			if(!isNull) return;
			let paramStr = JSON.stringify(params);
			console.log("退出提交打印》》》》："+paramStr)
			mui.neiosCommon.doLocalSql("platenProtectTab","NULL",'0','0',paramStr,function(ret){
				console.log(JSON.stringify(ret))
				if(ret == "localStorage Success"){
					mui.toast("已离线保存！")
					return;
				}
				if(ret == "INTEL"){
					mui.neiosRequst.httpRequst(that.url,params,function(ret){
						console.log(JSON.stringify(ret))
						if(ret.code == 0){
							mui.toast(ret.msg)
							setTimeout(that.goBack,700);
						}
						if( ret.code == 1){
								mui.toast(ret.msg)
						}
					},function(err){
						console.log("失败回掉："+JSON.stringify(err))
					})
				}
			})

		},
		reload(){
			SQLLITE.createTab("zqtab",function(ret){
				console.log("创建表"+JSON.stringify(ret))
			},function(err){
				console.log("创表失败"+JSON.stringify(err))
			})
		},
		Bdata(){
			let ste = {"transForm":{"status":"32","operateStaffCodeName":"[P129951]刘钰","operateStaffCode":"P129951","operateStaffName":"刘钰","tutelageStaffCode":"P626868","tutelageStaffName":"孔令新","tutelageStaffCodeName":"[P626868]孔令新","transOperateId":"bc8a8b8ab19ac8828b030a2aa69baa8a","type":"add","modifyBy":"P129951","modifyStaffName":"刘钰","areaCompanyCode":"50315939","stationCode":"50322817","dataStatus":"1","dataSource":"NEIOS_APP"+localStorage.getItem("currVersion"),"client":"NEIOS","createBy":"P129951","createStaffName":"刘钰","newAreaCode":"","newStationCode":"1020","workId":"739b9a24a88b48afab6c0adbb1b99488","workDesc":"对变压器有载调压信息进行确认"},"infoListParam":[{"transOperateInfoId":"12a6bb64a988a8a09a5b28e880a8898b","transOperateId":"bc8a8b8ab19ac8828b030a2aa69baa8a","operateDate":"2022-01-10 11:22","client":"NEIOS","dataSource":"NEIOS_APP"+localStorage.getItem("currVersion"),"status":"32","dataStatus":"1","createBy":"P129951","createStaffName":"刘钰","createDate":1641784920000,"modifyBy":"P129951","modifyStaffName":"刘钰","modifyDate":1641784920000,"reserveField1":null,"reserveField2":null,"reserveField3":null,"reserveField4":null,"reserveField5":null,"isLumpFirst":false,"isLumpFirstAdd":true,"selectText":"站用变压器","transType":"2","transTypeText":"站用变压器","deviceCode":"ghjk","beforeAdjustInfo":"5","afterAdjustInfo":"2","operateCount":"2","beforeI":"5","afterI":"5","highV":"5","lowV":"5","beforeHighV":"5","afterHighV":"5","beforeLowV":"5","afterLowV":"2","remark":"555"}],"pageType":"0","workType":"0206"}
			let strJSON = JSON.stringify(ste)
			mui.neiosCommon.doLocalSql('zqtab',null,'1','3',strJSON,function(ret){
				console.log("修改操作回调"+JSON.stringify(ret))
			})
		},
		search(){
			mui.neiosCommon.getLocalData("platenProtectTab",function(ret){
				console.log("查询表"+JSON.stringify(ret))
				JSON.parse(ret[0].data)
			},function(err){
				console.log("查询表失败"+JSON.stringify(err))
			})
		},
		dele(){
			SQLLITE.deleteTab(function(ret){
				console.log("删除表"+JSON.stringify(ret))
			},function(err){
				console.log("删除表失败"+JSON.stringify(err))
			})
		},
		tabCear(){
			mui.neiosCommon.clearTab('zqtab',function(ret){
				console.log("清空表"+JSON.stringify(ret))
			},function(err){
				console.log("清空表失败"+JSON.stringify(err))
			})
		},
		closDb(){
			SQLLITE.closeDB(function(ret){
				console.log("关闭数据库"+JSON.stringify(ret))
			},function(err){
				console.log("关闭数据库失败"+JSON.stringify(err))
			})
		},
		openDb(){
			SQLLITE.openDB(function(ret){
				console.log("打开数据库"+JSON.stringify(ret))
			},function(err){
				console.log("打开数据库失败"+JSON.stringify(err))
			})
		},
		chooesFile(){
				mui.neiosCommon.doUploads(function(ret){
					if("0" == ret.code){
						console.log("成功："+JSON.stringify(ret))
						mui.toast('文件'+ret.data.fileName+"上传成功");
						that.fileIds == "" ? that.fileIds += ret.data.fileId : that.fileIds += ","+ret.data.fileId;
						
						let fileArray = that.fileList;
						let data = {fileName:ret.data.fileName,fileid:ret.data.fileId,upLoadDate:ret.data.uploadTime,uploadPersion:ret.data.uploadUserName,doOne:""};
						fileArray.push(data)
					}else{
						mui.toast('文件上传失败');
					}
				},function(err){
					mui.toast('文件上传失败');
				})
				
		},
		FileDelete(position){
			mui.confirm("确认删除?",'提示',['取消','确认'],function (e) {
				if(1 == e.index){
					mui.toast("删除文件"+that.fileList[position].fileName)	
					//获取所有已记录id的默认长度 用于对比验证
					let idLength = that.fileIds.length;
					let idStr;
					let id = that.fileList[position].fileid+","
					idStr = that.fileIds.replace(id,"")
					//获取已记录id的最新长度
					let newidLength = idStr.length;
					//验证长度 如果长度没有变化 则认为没有进行 删除id 
					if(idLength == newidLength){
						let ids = that.fileList[position].fileid
						idStr = that.fileIds.replace(ids,"")
					}
					//删除末尾的 “,”
					let Tm = idStr.substring(idStr.length-1)
					if(Tm == ","){
						idStr = idStr.substring(0,idStr.length-1)
					}
					that.fileIds = idStr;
					console.log(that.fileIds)
					that.fileList.splice(position,1)
				}
			},'div')
		},
		downloadFile(position){
			let url = "/app/common/downloadFile";
			let param = {};
			param.fileId = that.fileList[position].fileid;
			mui.neiosCommon.downLoadFile(param,function(ret){
				mui.plusReady(function () {
					plus.nativeUI.closeWaiting();
				})
				//调用存储
				var callbackID = plus.bridge.callbackId(function(ret){
					mui.toast(ret[0])
				}, function(err){
					mui.toast('文件下载失败')
				});
				let fileName = that.fileList[position].fileName
				plus.bridge.execSync( "FileHelper", "doFileSave", [callbackID, ret, fileName, "Argus3", "Argus4"] )
			},function(err){
				mui.plusReady(function () {
					plus.nativeUI.closeWaiting();
				})
				mui.toast('文件下载失败')
			});
	
		},
	},
	
	})

	mui.init()
	projectVM.dtPickrer = new mui.DtPicker({
		type : 'datetime'
	})